function close_models3() {
    document.getElementById("addForm").style.display = "none";
}

function fill_val1() {
    document.getElementById("model-affirm1").setAttribute("onclick", "choose_people1()")
    document.getElementById("addForm").style.display="table-row";
    sessionStorage.clear();
    document.getElementById("an_branch1").style.color = "#01aaed";
    document.getElementById("an_rank1").style.color = "#0b0b0b";
    document.getElementById("an_func1").style.color = "#0b0b0b";
    document.getElementById("all_select_checkbox2").checked = false;
    var check_box_list = document.getElementsByClassName("check_box");
    document.getElementById("search_button2").setAttribute("onclick", "search_data1()");
    document.getElementById("an_branch1").setAttribute("onclick", "change_left_val1(this.id)");
    document.getElementById("an_rank1").setAttribute("onclick", "change_left_val1(this.id)");
    document.getElementById("an_func1").setAttribute("onclick", "change_left_val1(this.id)");
    for (var t = 0; t < check_box_list.length; t++) {
        document.getElementById(check_box_list[t].id).checked = false;
    }
    document.getElementById("checked-label2").innerHTML = "";

    deltr10();
    document.getElementById("img-display2").style.display = "inline-block";
    if (window.branch_list) {
        var branch_list = window.branch_list;
        var department_list1 = document.getElementById("department_list1");
        department_list1.innerHTML = "";
        var ul_tag = document.createElement("ul");
        ul_tag.style.fontSize = "0";
        ul_tag.style.margin = "0";
        ul_tag.style.padding = "0";
        for (var i = 0; i < branch_list.length; i++) {
            var li_tag = document.createElement("li");
            li_tag.style.fontSize = "14px";
            li_tag.style.margin = "0";
            li_tag.style.padding = "0";li_tag.style.height = "30px";
            li_tag.innerHTML = "<p style='margin-left: 20%;'>" + branch_list[i] + "</p>";
            li_tag.setAttribute("onclick", "choice_peopel12('" + branch_list[i] + "')");
            li_tag.setAttribute("id", branch_list[i]);
            li_tag.className = "li-class";
            ul_tag.appendChild(li_tag);
            department_list1.appendChild(ul_tag);
        }
    } else {
        var xhr = new XMLHttpRequest();
        var local_host = window.location.host;          // 获取当前IP
        xhr.open('post', "/technological/delegation", true);
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        xhr.send();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var JsonDate = JSON.parse(xhr.responseText);
                    window.personnel_list = JsonDate["data"];
                    window.emp_info_dict = JsonDate["emp_info_dict"];
                    window.people_dict = JsonDate["people_dict"];
                    window.people_dict_new = JsonDate["people_dict_new"];
                    window.branch_list = JsonDate["branch_list"];
                    window.function_list = JsonDate["function_list"];
                    var department_list1 = document.getElementById("department_list1");
                    department_list1.innerHTML = "";
                    var ul_tag = document.createElement("ul");
                    ul_tag.style.fontSize = "0";
                    ul_tag.style.margin = "0";
                    ul_tag.style.padding = "0";
                    for (var i = 0; i < JsonDate["branch_list"].length; i++) {
                        var li_tag = document.createElement("li");
                        li_tag.style.fontSize = "14px";
                        li_tag.style.margin = "0";
                        li_tag.style.padding = "0";
                        li_tag.innerHTML = "<p style='margin-left: 20%;'>" + JsonDate["branch_list"][i] + "</p>";
                        li_tag.setAttribute("onclick", "choice_peopel12('" + JsonDate["branch_list"][i] + "')");
                        li_tag.setAttribute("id", JsonDate["branch_list"][i]);
                        li_tag.className = "li-class";
                        ul_tag.appendChild(li_tag);
                        department_list1.appendChild(ul_tag);
                    }
                }
            }
        }
    }
}

function search_data1() {
    var searchData = document.getElementById("search_data2").value;
    var emp_info_dict = window.emp_info_dict;
    console.log(emp_info_dict);
    var searchValNum = 1;
    var department_list1 = document.getElementById("department_list1");
    document.getElementById("all_select_checkbox2").checked = false;
    document.getElementById("all_select_checkbox2").setAttribute("onclick", "check_all_select1('all')");
    document.getElementById("img-display2").style.display = "none";
    var t_table = document.getElementById("t_table3");
    t_table.innerHTML="";
    for (var i in emp_info_dict){
        for (var j=0; j<emp_info_dict[i].length; j++){
            if (emp_info_dict[i][j]["real_name"].indexOf(searchData) !== -1){
                var elm = emp_info_dict[i];
                var tr = document.createElement("tr");
                var td = document.createElement("td");
                var td1 = document.createElement("td");
                var td2 = document.createElement("td");
                var td3 = document.createElement("td");
                td.innerHTML = "<input id='" + elm[j].real_name + "(" + elm[j].job_number + ')' + "' class='check_box' type=\"checkbox\" onclick='choice_consignor1(this.id, \"" + i + "\")'>";
                td.style.textAlign = "center";
                td1.innerHTML = searchValNum;
                td1.style.textAlign = "center";
                td2.innerHTML = elm[j].real_name;
                td2.title = elm[j].real_name;
                td2.style.textAlign = "center";
                td3.innerHTML = elm[j].job_number;
                td3.title = elm[j].job_number;
                td3.style.textAlign = "center";
                tr.appendChild(td);
                tr.appendChild(td1);
                tr.appendChild(td2);
                tr.appendChild(td3);
                t_table.appendChild(tr);
                searchValNum += 1;
            }
        }
    }
}

function change_left_val1(ele_id) {
    if (ele_id === "an_rank1") {
        document.getElementById("an_branch1").style.color = "#0b0b0b";
        document.getElementById("an_func1").style.color = "#0b0b0b";
        document.getElementById(ele_id).style.color = "#01aaed";
        deltr10();
        document.getElementById("img-display2").style.display = "inline-block";
        document.getElementById("department_list1").innerHTML = "";
        var new_branch_list = ["书记、院长:A", "院领导:B", "职能科室负责人:C", "临床医技科室负责人:D", "副主任:G", "护士长:E","普通员工:F"];
        var new_department_list1 = document.getElementById("department_list1");
        var new_ul_tag = document.createElement("ul");
        new_ul_tag.style.fontSize = "0";
        new_ul_tag.style.margin = "0";
        new_ul_tag.style.padding = "0";
        for (var EF = 0; EF < new_branch_list.length; EF++) {
            var new_li_tag = document.createElement("li");
            new_li_tag.style.fontSize = "14px";
            new_li_tag.style.margin = "0";
            new_li_tag.style.padding = "0";
            new_li_tag.style.paddingLeft = "5%";
            new_li_tag.innerHTML = "<p>" + new_branch_list[EF].split(":")[0] + "</p>";
            new_li_tag.setAttribute("onclick", "choice_peopel_11('" + new_branch_list[EF].split(":")[1] + "')");
            new_li_tag.setAttribute("id", new_branch_list[EF].split(":")[1]);
            new_li_tag.className = "li-class";
            new_ul_tag.appendChild(new_li_tag);
            new_department_list1.appendChild(new_ul_tag);
        }
    } else if (ele_id === 'an_func1') {
        document.getElementById("an_branch1").style.color = "#0b0b0b";
        document.getElementById("an_rank1").style.color = "#0b0b0b";
        document.getElementById(ele_id).style.color = "#01aaed";
        deltr10();
        document.getElementById("img-display2").style.display = "inline-block";
        document.getElementById("department_list1").innerHTML = "";
        var department_list1_1 = document.getElementById("department_list1");
        var ul_tag_1 = document.createElement("ul");
        ul_tag_1.style.fontSize = "0";
        ul_tag_1.style.margin = "0";
        ul_tag_1.style.padding = "0";
        var function_list = window.function_list;
        var func_list = [];
        for (var tt in function_list['function_dict']) {
            func_list.push(tt);
        }
        console.log(func_list)
        for (var AB = 0; AB < func_list.length; AB++) {
            var new_li_1 = document.createElement("li");
            new_li_1.style.fontSize = "14px";
            new_li_1.style.margin = "0";
            new_li_1.style.padding = "0";
            new_li_1.style.paddingLeft = "5%";
            new_li_1.innerHTML = "<p>" + func_list[AB] + "</p>";
            new_li_1.setAttribute("onclick", "choice_peopel_21('" + function_list['function_dict'][func_list[AB]] + "')");
            new_li_1.setAttribute("id", function_list['function_dict'][func_list[AB]]);
            new_li_1.className = "li-class";
            ul_tag_1.appendChild(new_li_1);
            department_list1_1.appendChild(ul_tag_1);
         }
    } else {
        document.getElementById("an_rank1").style.color = "#0b0b0b";
        document.getElementById("an_func1").style.color = "#0b0b0b";
        document.getElementById(ele_id).style.color = "#01aaed";
        deltr10();
        document.getElementById("img-display2").style.display = "inline-block";
        document.getElementById("department_list1").innerHTML = "";
        var branch_list = window.branch_list;
        var department_list1 = document.getElementById("department_list1");
        department_list1.innerHTML = "";
        var ul_tag = document.createElement("ul");
        ul_tag.style.fontSize = "0";
        ul_tag.style.margin = "0";
        ul_tag.style.padding = "0";
        for (var i = 0; i < branch_list.length; i++) {
            var li_tag = document.createElement("li");
            li_tag.style.fontSize = "14px";
            li_tag.style.margin = "0";
            li_tag.style.padding = "0";
            li_tag.innerHTML = "<p style='margin-left: 20%;'>" + branch_list[i] + "</p>";
            li_tag.setAttribute("onclick", "choice_peopel12('" + branch_list[i] + "')");
            li_tag.setAttribute("id", branch_list[i]);
            li_tag.className = "li-class";
            ul_tag.appendChild(li_tag);
            department_list1.appendChild(ul_tag);
        }
    }
}

// 创建右侧选择人员列表
function choice_peopel_11(choice_key, chose_id) {
    var data_str = sessionStorage.getItem(choice_key);
    var data_list = [];
    if (data_str) {
        data_list = data_str.split(",")
    }
    var people_dict = window.people_dict;
    var people_dict_new = window.people_dict_new;
    document.getElementById("img-display2").style.display = "none";
    var t_table = document.getElementById("t_table3");
    if (document.getElementsByClassName("li-class add_background").length > 0) {
        var old_tag_id = document.getElementsByClassName("li-class add_background")[0].id;
        document.getElementById(old_tag_id).className = "li-class";
    }
    document.getElementById(choice_key).className = "li-class add_background";
    deltr10();
    document.getElementById("all_select_checkbox2").checked = false;
    document.getElementById("all_select_checkbox2").setAttribute("onclick", "check_all_select1('" + choice_key + "', 'executor2')")
    for (var i = 0; i < people_dict_new[choice_key].length; i++) {
        var elm = people_dict_new[choice_key];
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        td.innerHTML = "<input id='" + elm[i].real_name + '(' + elm[i].job_number + ')' + "' class='check_box' type=\"checkbox\" onclick='choice_consignor1(this.id, \"" + chose_id + "\"" + ", \"" + choice_key + "\")'>";
        td.style.textAlign = "center";
        td1.innerHTML = i + 1;
        td1.style.textAlign = "center";
        td2.innerHTML = elm[i].real_name;
        td2.title = elm[i].real_name;
        td2.style.textAlign = "center";
        td3.innerHTML = elm[i].job_number;
        td3.title = elm[i].job_number;
        td3.style.textAlign = "center";
        tr.appendChild(td);
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        t_table.appendChild(tr);
    }
    var tttt = 0;
    if (data_list) {
        if (data_list[0] === "all") {
            document.getElementById("all_select_checkbox2").checked = true;
            tttt = 1
        }
    }
    for (var III = tttt; III < data_list.length; III++) {
        document.getElementById(data_list[III].trim()).checked = true;
    }
}

function choice_peopel12(choice_key, chose_id) {
    var data_str = sessionStorage.getItem(choice_key);
    var data_list = [];
    if (data_str) {
        data_list = data_str.split(",")
    }
    var emp_info_dict = window.emp_info_dict;
    document.getElementById("img-display2").style.display = "none";
    var t_table = document.getElementById("t_table3");
    if (document.getElementsByClassName("li-class add_background").length > 0) {
        var old_tag_id = document.getElementsByClassName("li-class add_background")[0].id;
        document.getElementById(old_tag_id).className = "li-class";
    }
    document.getElementById(choice_key).className = "li-class add_background";
    deltr10();
    document.getElementById("all_select_checkbox2").checked = false;
    document.getElementById("all_select_checkbox2").setAttribute("onclick", "check_all_select1('" + choice_key + "', '" + chose_id + "')");

    for (var i = 0; i < emp_info_dict[choice_key].length; i++) {
        var elm = emp_info_dict[choice_key]
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        td.innerHTML = "<input id='" + elm[i].real_name + "(" + elm[i].job_number + ')' + "' class='check_box' type=\"checkbox\" onclick='choice_consignor1(this.id, \"" + chose_id + "\"" + ", \"" + choice_key + "\")'>";
        td.style.textAlign = "center";
        td1.innerHTML = i + 1;
        td1.style.textAlign = "center";
        td2.innerHTML = elm[i].real_name;
        td2.title = elm[i].real_name;
        td2.style.textAlign = "center";
        td3.innerHTML = elm[i].job_number;
        td3.title = elm[i].job_number;
        td3.style.textAlign = "center";
        tr.appendChild(td);
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        t_table.appendChild(tr);
    }

    var tttt = 0;
    if (data_list) {
        if (data_list[0] === "all") {
            document.getElementById("all_select_checkbox2").checked = true;
            tttt = 1
        }
    }
    for (var III = tttt; III < data_list.length; III++) {
        document.getElementById(data_list[III].trim()).checked = true;
    }
}

// 删除列表
function deltr10() {
    var tb = document.getElementById('t_table3');
    var rowNum = tb.rows.length;
    for (i = 0; i < rowNum; i++) {
        tb.deleteRow(i);
        rowNum = rowNum - 1;
        i = i - 1;
    }
}

// 复选框全选
function check_all_select1(choice_key, chose_id) {
    var select_check = document.getElementById("all_select_checkbox2").checked;
    var all_check_box = document.getElementsByClassName("check_box");
    chose_id = 'executor2'
    if (chose_id === "executor2") {
        var tag_id = "executor_str";
    } else {
        tag_id = "executor_str1";
    }

    var executor_list = [];
    var executor_str = document.getElementById(tag_id);
    if (executor_str) {
        if (executor_str.innerText.trim()) {
            executor_list = executor_str.innerText.split(", ")
        }
    }

    var new_check_val_list = [];
    var check_val_list = [];
    for (var HH = 0; HH < all_check_box.length; HH++) {
        all_check_box[HH].checked = true;
        check_val_list.push(all_check_box[HH].id);
        if (executor_list.lastIndexOf(all_check_box[HH].id) === -1) {
            new_check_val_list.push(all_check_box[HH].id)
        }
    }

    if (select_check) {
        if (chose_id === "executor2" || chose_id === "executor4") {

            if (executor_str) {
                if (executor_str.innerText.trim()) {
                    document.getElementById(tag_id).innerHTML = executor_str.innerText + ", " + new_check_val_list.join(", ");
                } else {
                    document.getElementById(tag_id).innerHTML = new_check_val_list.join(", ");
                }

            } else {
                document.getElementById("checked-label2").innerHTML = "<span id='" + tag_id + "' style='margin-left: 1%;width: 100%;'>" + check_val_list.join(", ") + "</span>";
            }
            sessionStorage.setItem(choice_key, "all" + ", " + check_val_list.join(", "))
        } else {
            alert("该节点不能多选！");
            document.getElementById("all_select_checkbox2").checked = false;
        }
    } else {
        sessionStorage.removeItem(choice_key);
        check_val_list = [];
        for (var II = 0; II < all_check_box.length; II++) {
            all_check_box[II].checked = false;
            check_val_list.push(all_check_box[II].id);
        }
        var old_check_val = document.getElementById(tag_id).innerHTML.split(", ");
        console.log(old_check_val)
        for (var TB = 0; TB < check_val_list.length; TB++) {
            removeByValue1(old_check_val, check_val_list[TB])
        }

        document.getElementById(tag_id).innerHTML = old_check_val.join(", ");
    }
}

function removeByValue1(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] === val) {
            arr.splice(i, 1);
            break;
        }
    }
}

// 选择委托人
function choice_consignor1(ele_id, chose_id, choice_key) {
    chose_id = 'executor2';
    if (chose_id === "executor2" || chose_id === "executor4") {
        var dataStr = sessionStorage.getItem(choice_key);
        var dataList = [];
        if (dataStr) {
            sessionStorage.setItem(choice_key, dataStr + ", " + ele_id)
        } else {
            sessionStorage.setItem(choice_key, ele_id)
        }

        if (chose_id === "executor2") {
            var tag_id = "executor_str";
        } else {
            tag_id = "executor_str1";
        }
        var executor_str = document.getElementById(tag_id);
        if (executor_str) {
            if (document.getElementById(ele_id).checked) {
                if (executor_str.innerText.trim()) {
                    document.getElementById(tag_id).innerHTML = executor_str.innerText + ", " + ele_id;
                } else {
                    document.getElementById(tag_id).innerHTML = ele_id;
                }
            } else {
                var str_push = [];
                var executor_list = executor_str.innerText.split(", ");
                for (var kl = 0; kl < executor_list.length; kl++) {
                    if (executor_list[kl] !== ele_id) {
                        str_push.push(executor_list[kl])
                    }
                }
                document.getElementById(tag_id).innerHTML = str_push.join(", ")
            }
        } else {
            document.getElementById("checked-label2").innerHTML = "<span id='" + tag_id + "' style='margin-left: 1%;width: 100%;'>" + ele_id + "</span>";
        }
    } else {
        check_more_one_not(ele_id);
        document.getElementById("checked-label2").innerHTML = "<span id='" + tag_id + "' style='margin-left: 1%;'>" + ele_id + "</span>";
    }

}

function choose_people1() {
    var executor111 = '';
    var checked_label = document.getElementById("checked-label2");
    for (var j = 0; j < checked_label.children.length; j++) {
        executor111 = executor111 + checked_label.children[j].innerText.trim() + ", ";
    }
    document.getElementById("addForm").style.display = "none";
    var executor = executor111.slice(0, -2);
    document.getElementById('SelectPersonnel').innerText = executor;
}

// 创建右侧选择人员列表
function choice_peopel_21(choice_key, chose_id) {
    var data_str = sessionStorage.getItem(choice_key);
    var data_list = [];
    if (data_str) {
        data_list = data_str.split(",")
    }

    var people_dict_new = window.function_list;
    document.getElementById("img-display2").style.display = "none";
    var t_table = document.getElementById("t_table3");
    if (document.getElementsByClassName("li-class add_background").length > 0) {
        var old_tag_id = document.getElementsByClassName("li-class add_background")[0].id;
        document.getElementById(old_tag_id).className = "li-class";
    }
    document.getElementById(choice_key).className = "li-class add_background";
    deltr10();
    document.getElementById("all_select_checkbox2").checked = false;
    document.getElementById("all_select_checkbox2").setAttribute("onclick", "check_all_select1('" + choice_key + "', 'executor2')")
    console.log(people_dict_new)
    for (var i = 0; i < people_dict_new[choice_key].length; i++) {
        var elm = people_dict_new[choice_key];
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        td.innerHTML = "<input id='" + elm[i].real_name + '(' + elm[i].job_number + ')' + "' class='check_box' type=\"checkbox\" onclick='choice_consignor1(this.id, \"" + chose_id + "\"" + ", \"" + choice_key + "\")'>";
        td.style.textAlign = "center";
        td1.innerHTML = i + 1;
        td1.style.textAlign = "center";
        td2.innerHTML = elm[i].real_name;
        td2.title = elm[i].real_name;
        td2.style.textAlign = "center";
        td3.innerHTML = elm[i].job_number;
        td3.title = elm[i].job_number;
        td3.style.textAlign = "center";
        tr.appendChild(td);
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        t_table.appendChild(tr);
    }
    var tttt = 0;
    if (data_list) {
        if (data_list[0] === "all") {
            document.getElementById("all_select_checkbox2").checked = true;
            tttt = 1
        }
    }
    for (var III = tttt; III < data_list.length; III++) {
        document.getElementById(data_list[III].trim()).checked = true;
    }
}